package com.hmkj.admin.controller.order;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.utils.AdminSessionHelper;
import com.hmkj.common.baseMapper.GenericPo;
import com.hmkj.common.utils.BigDecimalUtils;
import com.hmkj.core.po.operate.Agent;
import com.hmkj.core.po.order.Order;
import com.hmkj.core.po.order.OrderRelation;
import com.hmkj.core.po.system.Admin;
import com.hmkj.core.po.user.User;
import com.hmkj.core.service.investor.InvestorService;
import com.hmkj.core.service.operate.AgentService;
import com.hmkj.core.service.order.OrderRelationService;
import com.hmkj.core.service.order.OrderService;
import com.hmkj.core.service.user.UserService;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Log4j
@Controller
@RequestMapping(BaseController.BASE_URI)
public class OrderController extends BaseController {

    @Resource
    OrderService orderService;
    @Resource
    UserService userService;
    @Resource
    OrderRelationService orderRelationService;
    @Resource
    AgentService agentService;


    /**
     * 策略列表
     * @return
     */
    @RequestMapping(value= "order/list", method = RequestMethod.GET)
    public String list() {
        return ok("order/list");
    }

    /**
     * 策略持仓中列表
     * @return
     */
    @RequestMapping(value= "order/position", method = RequestMethod.GET)
    public String position() {
        return ok("order/position");
    }

    /**
     * 策略委托中列表
     * @return
     */
    @RequestMapping(value= "order/entrust", method = RequestMethod.GET)
    public String entrust() {
        return ok("order/entrust");
    }

    /**
     * 策略结算中列表
     * @return
     */
    @RequestMapping(value= "order/balance", method = RequestMethod.GET)
    public String balance() {
        return ok("order/balance");
    }

    /**
     * 策略详情
     * @return
     */
    @RequestMapping(value= "order/detail", method = RequestMethod.GET)
    public String detail(ModelMap modelMap, Integer orderId) {

        //策略详情
        Order order = orderService.selectByPrimaryKey(orderId);
        modelMap.put("order",order);

        //点买人
        User user = userService.selectByPrimaryKey(order.getUserId());
        modelMap.put("user",user);

        //投资人
        User investor = userService.selectByPrimaryKey(order.getInvestId());
        modelMap.put("investor",investor);

        //代理商
        Agent agent = agentService.selectByPrimaryKey(order.getAgentId());
        modelMap.put("agent",agent);

        //点买人盈亏分配
        BigDecimal userProfit = BigDecimal.ZERO;
        //投资人盈亏分配
        BigDecimal investorProfit = BigDecimal.ZERO;
        //点买人亏损金额
        BigDecimal userLose = BigDecimal.ZERO;
        //投资人亏损金额
        BigDecimal investorLose = BigDecimal.ZERO;
        //返还点买人履约保证金
        BigDecimal backUserDeposit = BigDecimal.ZERO;
        //返还投资人止盈保证金
        BigDecimal backInvestorDeposit = BigDecimal.ZERO;

        if(BigDecimalUtils.isGreaterThan(order.getProfit(),BigDecimal.ZERO)){//盈利
            if (order.getProfit().compareTo(order.getProfitDeposit()) > 0) {//盈利大过止盈保证金
                userProfit = order.getProfitDeposit().multiply(order.getAllotRate()).setScale(2,BigDecimal.ROUND_HALF_UP);
                investorProfit = order.getProfit().subtract(userProfit).setScale(2,BigDecimal.ROUND_HALF_UP);
            } else {
                userProfit = order.getProfit().multiply(order.getAllotRate()).setScale(2,BigDecimal.ROUND_HALF_UP);
                investorProfit = order.getProfit().subtract(userProfit).setScale(2,BigDecimal.ROUND_HALF_UP);
            }
            backUserDeposit = order.getDeposit();
            backInvestorDeposit = order.getProfitDeposit().subtract(userProfit).setScale(2,BigDecimal.ROUND_HALF_UP);

        }else{//亏损
            if (order.getProfit().negate().compareTo(order.getDeposit()) == 1) { //穿仓
                userProfit = order.getDeposit();
                investorProfit = order.getProfit().add(order.getDeposit());
                userLose = order.getDeposit();
                investorLose = order.getProfit().add(order.getDeposit());
                backInvestorDeposit = order.getProfitDeposit().add(investorLose);
            } else {
                userProfit = order.getProfit();
                userLose = order.getProfit();
                backUserDeposit = order.getDeposit().add(userLose);
                backInvestorDeposit = order.getProfitDeposit();
            }
        }

        //获取订单号前14位
        String orderNo = order.getOrderNo().substring(0,14);
        //对应模拟订单号
        Integer simId = 0;
        //获取跟单对应模拟订单
        if(order.getFollow()==Order.FOLLOW.T2.code){
            OrderRelation relation = orderRelationService.selectOne(new OrderRelation(t->{
                t.setRealOrderNo(orderNo);
                t.setDelFlag(OrderRelation.DELFLAG.NORMAL.code);
            }));
            if(null != relation){
                simId = relation.getSimId();
            }
        }

        modelMap.put("userProfit",userProfit);
        modelMap.put("investorProfit",investorProfit);
        modelMap.put("userLose",userLose);
        modelMap.put("backUserDeposit",backUserDeposit);
        modelMap.put("investorLose",investorLose);
        modelMap.put("backInvestorDeposit",backInvestorDeposit);
        modelMap.put("simId",simId);
        return ok("order/detail");
    }

    /**
     * 交易数据报表
     */
    @RequestMapping(value= "order/orderForm", method = RequestMethod.GET)
    public String orderForm(ModelMap modelMap) {
        //获取当前登录用户检验是否为代理商
        Agent agent = new Agent();
        agent.setDelFlag(Agent.DELFLAG.NORMAL.code);
        Admin admin = AdminSessionHelper.getCurrAdmin();
        if(admin.getType() == Admin.TYPE.T3.code){
            agent.setId(admin.getBussId());
        }
       List<Agent> agentList = agentService.select(agent);
        modelMap.put("agents",agentList);
        return ok("form/orderForm");
    }

}